package com.cn.wanxi.servlet.user;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.PageBean;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.UserService;
import com.cn.wanxi.service.impl.UserServiceImpl;
import com.cn.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/back/user/findAll")
public class UserFindAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        //取值，取得前端页面传递过来的数据
        String username = req.getParameter("username");
        String start = req.getParameter("start");
        String end = req.getParameter("end");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String hobby = req.getParameter("hobby");
        String enable = req.getParameter("enable");
        String sex = req.getParameter("sex");

        String page = req.getParameter("page");
        String pageSize = req.getParameter("pageSize");
//封装数据
        PageBean<UserModel> pageBean = new PageBean<>();
        pageBean.setPage(Tool.pageToInt(page));
        pageBean.setPageSize(Tool.pageSizeToInt(pageSize));
        UserModel userModel = new UserModel();
        userModel.setPageBean(pageBean);
        userModel.setUsername(Tool.nullToString(username));
        userModel.setStart(Tool.nullToString(start));
        userModel.setEnd(Tool.nullToString(end));
        userModel.setEnable(Tool.enableToInt(enable));
        userModel.setEmail(Tool.nullToString(email));
        userModel.setPhone(Tool.nullToString(phone));
        userModel.setHobby(Tool.nullToString(hobby));
        userModel.setSex(Tool.nullToString(sex));


//        判断验证码是否输入正确，时属于逻辑关系，应该写在服务逻辑层
//        调用服务逻辑层
        UserService userService = new UserServiceImpl();
        PageBean<UserModel> bean = userService.findAll(userModel);
//        取得返回结果,如果参数大于两个，且类型一致，都是属于同一个对象--和数据库的表一一对应
//        List<UserModel> list = userService.findAll(userModel);
//返回数据json对象的格式,如果要用json格式，我们需要加入第三方jar包
//        resp.getWriter().println();
        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("count",userService.getCount(userModel));
        jsonObject.put("bean", bean);
        resp.getWriter().println(jsonObject);
    }
}
